package org.chris.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(DemoProperties.class)
public class DemoAutoConfiguration {

    @Autowired
    private DemoProperties demoProperties;

    @Bean
    public DemoService demoService(){
        DemoService demoService = new DemoService();
        demoService.setDemoProperties(demoProperties);
        return demoService;
    }

    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        // 直接获取ServletRegistrationBean对象 并关联自定义的servlet
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        // 设置servlet对应的 UrlMapping
        bean.addUrlMappings("/second");
        return bean;
    }
}
